package factory;

import beans.BeanDefinition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by inuyasha on 18-3-22.
 */
public abstract class AbstractBeanFactory implements ConfigurableListableBeanFactory{

    private Map<String,Object> map = new ConcurrentHashMap<String,Object>();

    public Object getBean(String name) {
        Object o = map.get(name);
        if(o != null)
            return o;
        BeanDefinition beanDefinition = getBeanDefinitionByName(name);
        if(beanDefinition == null){
            throw new RuntimeException("bean definition :" + name + " is not registered");
        }

        o = doCreateBean(beanDefinition);
        this.map.put(name, o);
        return o;
    }
    protected abstract BeanDefinition getBeanDefinitionByName(String beanName);

    protected abstract Object doCreateBean(BeanDefinition beanDefinition);
}
